package com.snowfield.lbcommon.utils.commonsexec;

import org.apache.commons.exec.LogOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * 用于打印执行命令过程中的输出日志等信息
 *
 * @author snowfield
 */
public class LogOutput extends LogOutputStream {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogOutput.class);

    public static final int ERROR_LEVEL = 0;//错误信息
    public static final int INPUT_LEVEL = 1;//输入信息
    public static final int OUTPU_TLEVEL = 2;//输出信息

    public LogOutput(int level) {
        super(level);
    }

    @Override
    protected void processLine(String line, int level) {
        String header = "";
        switch (level) {
            case ERROR_LEVEL:
                header = "ERR";
//                LOGGER.error("{} : {}", header, line);
                LOGGER.error("{} : {}", header, new String(line.getBytes(Charset.defaultCharset()), StandardCharsets.UTF_8));
                break;
            case INPUT_LEVEL:
                header = "IN";
                LOGGER.info("{} : {}", header, line);
//                LOG.error("{} : {}", header, new String(line.getBytes(Charset.defaultCharset()),"UTF-8"));
                break;
            case OUTPU_TLEVEL:
                header = "OUT";
                LOGGER.info("{} : {}", header, line);
//                LOG.error("{} : {}", header, new String(line.getBytes(Charset.defaultCharset()),"UTF-8"));
                break;
        }
    }
}
